package com.zkjl.protect_business.utils;

import cn.hutool.extra.pinyin.PinyinUtil;
import com.zkjl.protect_business.dto.PageBaseDTO;
import com.zkjl.protect_business.dto.SortDTO;
import com.zkjl.protect_business.entity.Notice;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;

import static com.zkjl.protect_business.common.Consts.INDEX_PREFIX;

/**
 * 构建查询工具类
 */
public class QueryUtil {


    /**
     * 创建索引
     * @param name 当前索引中文名
     * @param pname 父级中文名称
     * @return
     */
    public static IndexCoordinates createIndex(String name, String pname) {

        String pinyin = PinyinUtil.getPinyin(name, "");
        if (StringUtils.isNotBlank(pname)) {
            String ppinyin = PinyinUtil.getPinyin(pname, "");
            IndexCoordinates indexCoordinates = IndexCoordinates.of(INDEX_PREFIX + ppinyin + "_" + pinyin);
            return indexCoordinates;
        }else {
            IndexCoordinates indexCoordinates = IndexCoordinates.of(INDEX_PREFIX + pinyin);
            return indexCoordinates;
        }

    }


    public static <T> Pageable build(BoolQueryBuilder qb,NativeSearchQueryBuilder queryBuilder, PageBaseDTO pageBaseDTO, SortDTO sortDTO,Class<T> obj) {

        //全局关键词
        String search = pageBaseDTO.getSearch();
        if (StringUtils.isNotEmpty(search)) {
            //精确匹配  https://blog.csdn.net/hereiskxm/article/details/47979013
            MultiMatchQueryBuilder type = new MultiMatchQueryBuilder(search).type(MultiMatchQueryBuilder.Type.PHRASE).slop(0);
            qb.filter(type);
        }

        Long startTime = pageBaseDTO.getStartTime();
        Long endTime = pageBaseDTO.getEndTime();
        if (startTime != null && startTime != 0) {
            qb.filter(QueryBuilders.rangeQuery("create_time").gte(startTime));
        }
        if (endTime != null && endTime != 0) {
            qb.filter(QueryBuilders.rangeQuery("create_time").lt(endTime));
        }

        Sort.Direction directionSort = Sort.Direction.DESC;
        String direction = sortDTO.getSort();
        if (StringUtils.isNotEmpty(direction)) {
            String lowerCase = direction.toLowerCase();
            if ("desc".equals(lowerCase)) {
                directionSort = Sort.Direction.DESC;
            } else if ("asc".equals(lowerCase)) {
                directionSort = Sort.Direction.ASC;
            }
        }

        String field = sortDTO.getOrder();
        if (StringUtils.isEmpty(field)) {
            field = "create_time";
        }else {
            field = BeanUtil.isString(obj, field);

        }

        //分页
        Pageable pageable = PageRequest.of(pageBaseDTO.getPageNum() - 1, pageBaseDTO.getPageSize(),directionSort, field);
        queryBuilder.withPageable(pageable);

        return pageable;
    }


}
